package org.vsg.security.model.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;

import org.vsg.common.biz.exception.ServiceException;
import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.PaginationException;
import org.vsg.common.pagination.carrier.Carrier;
import org.vsg.common.pagination.carrier.RecordLimitCarrier;
import org.vsg.common.pagination.datatrans.PaginationProvider;
import org.vsg.common.pagination.datatrans.PaginationProviderFactory;
import org.vsg.security.model.domain.SysResource;
import org.vsg.security.model.persistence.SysResourceDao;
import org.vsg.security.model.service.SysResourceService;


public class SysResourceServiceImpl implements SysResourceService {
	/**
	 * set the pagination provider factory
	 * 
	 * 
	 */
	private PaginationProviderFactory factory = PaginationProviderFactory
			.getInstance();

	private SysResourceDao sysResourceDao;

	/**
	 * @param carrier
	 * @return
	 * @exception ServiceException
	 * @exception PaginationException
	 */
	public Pagination pageSysResource(Carrier carrier) throws ServiceException,
			PaginationException {
		// TODO: implement
		PaginationProvider provider = factory
				.generateProvider((RecordLimitCarrier) carrier);
		return sysResourceDao.pageSysResource(provider);
	}

	/**
	 * @param carrier
	 * @return
	 * @throws ServiceException
	 * @exception PaginationException
	 */
	public Pagination pageSysResourceByAll(Carrier carrier) throws ServiceException,
			PaginationException {
		// TODO: implement
		PaginationProvider provider = factory
				.generateProvider((RecordLimitCarrier) carrier);
		return sysResourceDao.pageSysResourceByAll(provider);
	}
	
	@Override
	public void saveAll(Collection<SysResource> all)
			throws ServiceException {
		// TODO Auto-generated method stub
		sysResourceDao.saveAll(all);
		
	}  	

	/**
	 * @param sysResource
	 * @exception ServiceException
	 */
	public void addSysResource(SysResource sysResource) throws ServiceException {
		// TODO: implement
		sysResourceDao.save(sysResource);
	}

	/**
	 * @param sysResource
	 * @exception ServiceException
	 */
	public void updateSysResource(SysResource sysResource) throws ServiceException {
		// TODO: implement
		sysResourceDao.update(sysResource);
	}

	/**
	 * @param sysResource
	 * @exception ServiceException
	 */
	public void updateSimpleSysResource(SysResource newEntity) throws ServiceException {
		// TODO: implement
		SysResource original = sysResourceDao.load( newEntity.getId() );
		
		original.setId( newEntity.getId()  );
		
		// --- update the method ---
		sysResourceDao.update( original );		
		
	}

	/**
	 * @param sysResource
	 * @exception ServiceException
	 */
	public void deleteSysResource(SysResource sysResource) throws ServiceException {
		// TODO: implement
		sysResourceDao.delete(sysResource);
	}

	@Override
	public void deleteAll(Collection<SysResource> all)
			throws ServiceException {
		// TODO Auto-generated method stub
		sysResourceDao.deleteAll(all);
	}
	
	@Override
	public void markDeleteSysResource(SysResource sysResource) throws ServiceException {
		// TODO Auto-generated method stub
		SysResource loadSysResource = sysResourceDao.load( sysResource.getId() );
		loadSysResource.setFlagDeleted( (short)1 );
		sysResourceDao.update( loadSysResource );
	}	

	/**
	 * @param pk
	 * @exception ServiceException
	 */
	public SysResource loadSysResource(Serializable pk) throws ServiceException {
		// TODO: implement
		return sysResourceDao.load(pk);
	}

	@Override
	public SysResource getByCode(String code) throws ServiceException {
		// TODO Auto-generated method stub
		Collection<SysResource> some = sysResourceDao.findByCode(code);
		SysResource sysResource = null;
		Iterator<SysResource> itera = some.iterator();
		if ( itera.hasNext() ) {
			sysResource = itera.next();
		}
		return sysResource;
	}

	@Override
	public Collection<SysResource> findByAll() throws ServiceException {
		// TODO Auto-generated method stub
		return sysResourceDao.findByAll();
	}


	public SysResourceDao getSysResourceDao() {
		return sysResourceDao;
	}

	/**
	 * @param newSysResourceDao
	 */
	public void setSysResourceDao(SysResourceDao newSysResourceDao) {
		sysResourceDao = newSysResourceDao;
	}




}